﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Collections;
using System.Data;
using System.Threading;
using HF.Service;
using HF.Framework.Log;
using ZX.SYS.Bussiness.Message;
using ZX.SYS.Library;
using ZX.Common.File;
using System.IO;
using System.Collections.Specialized;
using System.Collections;
using ZX.TR.Library;
using ZX.EHR.Library;
using ZX.TR.Bussiness;
using System.Configuration;
using ZX.BLL;
namespace HF.Service
{
    public class MailThread
    {
        string mailFrom = MailHelper.MailServerFrom;
        public string mailTo = string.Empty;
        public string mailSubject = string.Empty; 
        public string mailBody = string.Empty; 
        string mailAttch = string.Empty; 
        string mailCode = string.Empty; 
        string mailPriority = string.Empty; 
        string mailCC = string.Empty; 
        string resultMessage = "";
        public string messageId = "";
        public MailThread() { }

        public void Send()
        {
            lock (this)
            {
                string resultMessage = "";
                MailHelper.SendNetMail(mailFrom, mailTo, mailSubject, mailBody, mailAttch, mailPriority, mailCC, out resultMessage);
                string[] toMessage = resultMessage.Split(';');
                if (toMessage != null) 
                {
                    if (toMessage.Length > 0) 
                    {
                        if (toMessage[0] == "1") { LogData.WriteAppServerLog(toMessage[1], "2"); }
                        else
                        {
                            LogData.WriteLog("SendMailService错误:" + toMessage[1], "邮件服务");
                            LogData.WriteAppServerLog("SendMailService错误:" + toMessage[1], "4");
                        }
                    }
                
                }

            }
        }
    }
    public class SendMessageMailService
    {
        public  MailAddressCollection toAddress;
        private string MailTemplatePath = @"\mailtemplate\";
        public SendMessageMailService()
        {
            
        }

        /// <summary>
        /// 信息模板
        /// </summary>
        /// <returns></returns>
        private IList<string> MessageTemplate() 
        {
            IList<string> list = new List<string>();
            list.Add("V_TrActivityCourse");

            return list;
        }
        private void setSendFlag(Guid MPoolID)
        {
            new BDatMessagePool().setSendState(MPoolID);
        }
        public void Start()
        {
           
            //DebugHF.WriteServiceLog("SendMailService Runing...","2");


            IList<MessageItem> list = new BMessageItem().GetMailMessages();
            if (list != null && list.Count > 0)
            {
                foreach (MessageItem item in list)
                {

                    string path = AppDomain.CurrentDomain.BaseDirectory + MailTemplatePath; //CFile.GetPhyPath()+MailTemplatePath;
                    if (Directory.Exists(path))
                    {
                        switch (item.ViewName)
	                    {
                            case"V_TrActivityCourse":
                                V_TrActivityCourse_Mail(item, path);
                                break;
		                    default:
                                break;
	                    }
                      
                       
                     
                    }
                }
            }
            
        }

        private void V_TrActivityCourse_Mail(MessageItem item,string xPath) 
        {
            try
            {
                BDatMessageReceiver Bmr = new BDatMessageReceiver();
                IList<DatMessageReceiver> list = Bmr.GetReceivers(item.MsgID);
                if (list != null) 
                {
                    if (list.Count > 0) 
                    {
                        V_TrActivityCourse ac = new BV_TrActivityCourse().GetV_TrActivityCourseByID(item.QueryID);
                        
                        NameValueCollection mailCol = new NameValueCollection();

                        string link = ConfigurationManager.AppSettings["AppURL"];
                        link += item.MsgContent;
                        mailCol.Add("PlanStartTime", ac.PlanStartTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        mailCol.Add("PlanEndTime", ac.PlanEndTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        mailCol.Add("CourseName", ac.CourseName);
                        mailCol.Add("CourseContent", ac.CourseContent);
                        mailCol.Add("link", link);
                        foreach (DatMessageReceiver mr in list)
                        {
                           
                            string userEmail="";
                            DatUser u = new BUser().GetDatUserByUserId(mr.ReceiverID);
                          
                            mailCol.Add("ename", u.UName);
                            userEmail=u.Email;
                            //if (!string.IsNullOrEmpty(userEmail)) { continue; }


                            MailThread m = new MailThread();
                            m.mailSubject = item.Title;
                            string path = xPath+item.ViewName + ".txt";
                            if (File.Exists(path))
                            {
                                m.mailBody = TemplateHelper.BulidByFile(path, mailCol);
                                m.mailTo = userEmail;
                                m.messageId = item.MsgID.ToString();
                                Thread workerThread = new Thread(m.Send);
                                workerThread.Start();
                                new BDatMessagePool().setSendState(item.MPoolID);
                                Thread.Sleep(1000);
                            }
                        }
                    }
                
                }
        
            }
            catch (Exception ex)
            {

                throw ex;
            }
        
        }
    }
}
